//
//  YD3rdRegisterViewController.m
//  IngCard
//
//  Created by kf on 2017/8/28.
//  Copyright © 2017年 IngDan. All rights reserved.
//

#import "YD3rdRegisterViewController.h"
#import "YDAccountManager.h"
#import "YDAuthManager.h"
#import "LinkedInHelper.h"
#import "YD3rdInfoConfirmViewController.h"

@interface YD3rdRegisterViewController ()

@property(nonatomic, strong) UITextField *phoneTextField;
@property(nonatomic, strong) UIView *phoneLine;

@property(nonatomic, strong) UITextField *passwordTextField;
@property(nonatomic, strong) UIView *passwordLine;
@property(nonatomic, strong) UIButton *codeButton;

@property(nonatomic, strong) UIButton *confirmButton;
@property(nonatomic, strong) NSTimer *verifyCodeTimer;
@property(nonatomic, assign) NSInteger count;
@end

@implementation YD3rdRegisterViewController


- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [THEME renderStatusBarStyle:YDStatusBarStyle_Light];
    [THEME renderNavigationBar:self style:YDNavigationStyle_Green];
}

- (void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear: animated];
    [THEME renderStatusBarStyle:YDStatusBarStyle_Dark];
}

- (void) initSubviews
{
    [super initSubviews];
    
    self.title = STRING.user_3rd_register;
    
    
    [self.view addSubview:self.phoneLine];
    [self.phoneLine makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).offset(147);
        make.width.equalTo(251);
        make.height.equalTo(1);
    }];
    
    [self.view addSubview:self.phoneTextField];
    [self.phoneTextField makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.phoneLine.mas_left);
        make.bottom.equalTo(self.phoneLine.mas_top).offset(-2);
        make.width.equalTo(251);
        make.height.equalTo(25);
    }];
    
    
    [self.view addSubview:self.passwordTextField];
    [self.passwordTextField makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.phoneLine);
        make.top.equalTo(self.phoneLine.mas_bottom).offset(30);
        make.width.equalTo(109);
        make.height.equalTo(25);
    }];
    
    [self.view addSubview:self.passwordLine];
    [self.passwordLine makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.phoneLine);
        make.top.equalTo(self.passwordTextField.mas_bottom);
        make.width.equalTo(136);
        make.height.equalTo(1);
    }];
    
    [self.view addSubview:self.codeButton];
    [self.codeButton makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.passwordLine.mas_right).offset(12);
        make.bottom.equalTo(self.passwordLine);
        make.width.equalTo(103);
        make.height.equalTo(35);
    }];
    
    [self.view addSubview:self.confirmButton];
    [self.confirmButton makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.passwordLine.mas_bottom).offset(44);
        make.width.equalTo(251);
        make.height.equalTo(44);
    }];
    
}

#pragma mark - private

- (void)confirmButtonAction:(UIButton *)btn
{
    [self invalidateTimer];
    
    [AUTH authBindAccount:YD3rdSignID_LinkedIn
          accessToken:[LinkedInHelper sharedInstance].accessToken
               mobile:self.phoneTextField.text
           verifyCode:self.passwordTextField.text
           onComplete:^(BOOL success, NSString *msg) {
              if (success)
              {
                  YD3rdInfoConfirmViewController *vc = [[YD3rdInfoConfirmViewController alloc] init];
                  [self.navigationController pushViewController:vc animated:YES];
              }
              else
              {
                  [QMUITips showInfo:msg inView:self.view hideAfterDelay:2];
              }
           }];
}

- (void)codeButtonAction:(UIButton *)btn
{
    if(self.count > 0)
        return;
    
    [self setupTimer];
    [ACCOUNT getVerifyCode:self.phoneTextField.text
                  isLogion:NO
                onComplete:^(BOOL success, NSString *msg)
     {
         if (!success)
         {
             [self invalidateTimer];
             [QMUITips showWithText:msg inView:self.view hideAfterDelay:1];
         }
     }];
}


#pragma mark - Private
- (void)textFieldDidBeginChange:(UITextField *)textField
{
    if (self.phoneTextField.text.length >= 11) {
        self.phoneTextField.text = [self.phoneTextField.text substringToIndex:11];
        self.codeButton.enabled = YES;
    }
    else
    {
        self.codeButton.enabled = NO;
    }
    
    if (self.passwordTextField.text.length >= 4)
    {
        self.passwordTextField.text = [self.passwordTextField.text substringToIndex:4];
    }
    
    if (self.phoneTextField.text.length >= 11 && self.passwordTextField.text.length >= 4)
    {
        self.confirmButton.enabled = YES;
    }
    else
    {
        self.confirmButton.enabled = NO;
    }
}

- (void)setupTimer
{
    [self invalidateTimer];
    self.count = 59;
    self.codeButton.titleLabel.font = UIFontLightMake(12);
    [self.codeButton setTitle:NSStringFormat(STRING.user_resend,self.count) forState:UIControlStateNormal];
    self.codeButton.userInteractionEnabled = NO;
    self.verifyCodeTimer = [NSTimer scheduledTimerWithTimeInterval:1
                                                            target:self
                                                          selector:@selector(changeTime:)
                                                          userInfo:nil
                                                           repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.verifyCodeTimer forMode:NSRunLoopCommonModes];
}

- (void)invalidateTimer
{
    [self.verifyCodeTimer invalidate];
    self.verifyCodeTimer = nil;
    
    self.codeButton.userInteractionEnabled = YES;
    self.codeButton.titleLabel.font = UIFontLightMake(15);
    [self.codeButton setTitle:STRING.user_sendCode forState:(UIControlStateNormal)];
}

-(void)changeTime:(NSTimer *)timer
{
    if (self.count == 1) {
        [self invalidateTimer];
    }else{
        self.count--;
        self.codeButton.titleLabel.font = UIFontLightMake(12);
        [self.codeButton setTitle:NSStringFormat(STRING.user_resend,self.count) forState:UIControlStateNormal];
    }
}

#pragma mark -  lazy loading

-(UITextField *) phoneTextField
{
    if(!_phoneTextField)
    {
        _phoneTextField = [[UITextField alloc]init];
        _phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
        
        [_phoneTextField setStyle:^(UIView *target) {
            UITEXTFIELDSTYLE;
            temp.font = UIThinFontMake(18);
            temp.textColor = UIColorMake(121, 121, 121);
            temp.textAlignment = NSTextAlignmentLeft;
            temp.placeholder = STRING.user_phoneNumber;
        }];
        
        [_phoneTextField addTarget:self
                            action:@selector(textFieldDidBeginChange:)
                  forControlEvents:UIControlEventEditingChanged];
        _phoneTextField.delegate = self;
    }
    return _phoneTextField;
}
-(UIView *)phoneLine
{
    if (!_phoneLine)
    {
        _phoneLine = [[UIView alloc] init];
        _phoneLine.backgroundColor = UIColorMake(151, 151, 151);
    }
    return _phoneLine;
}

- (UITextField *) passwordTextField
{
    if (!_passwordTextField)
    {
        _passwordTextField = [[UITextField alloc]init];
        _passwordTextField.keyboardType = UIKeyboardTypeNumberPad;
        _passwordTextField.frame = CGRectMake(0, 0, 136, 25);
        
        [_passwordTextField setStyle:^(UIView *target) {
            UITEXTFIELDSTYLE;
            temp.font = UIThinFontMake(18);
            temp.textColor = UIColorMake(121, 121, 121);
            temp.textAlignment = NSTextAlignmentLeft;
        }];
        [_passwordTextField addTarget:self
                               action:@selector(textFieldDidBeginChange:)
                     forControlEvents:UIControlEventEditingChanged];
        _passwordTextField.delegate = self;
    }
    return _passwordTextField;
}

- (UIView *) passwordLine
{
    if (!_passwordLine)
    {
        _passwordLine = [[UIView alloc] init];
        _passwordLine.backgroundColor = UIColorMake(151, 151, 151);
    }
    return _passwordLine;
}

-(UIButton *) codeButton
{
    if (!_codeButton)
    {
        _codeButton = [[UIButton alloc] init];
        _codeButton.frame = CGRectMake(0, 0, 103, 35);
        _codeButton.layer.borderColor = UIColorGray.CGColor;
        _codeButton.layer.borderWidth = 1;
        _codeButton.layer.cornerRadius = 4;
        _codeButton.layer.masksToBounds = YES;
        _codeButton.enabled = NO;
        
        [_codeButton setStyle:^(UIView *target) {
            UIBUTTONSTYLE;
            [temp setTitle:STRING.user_sendCode forState:(UIControlStateNormal)];
            temp.titleLabel.font = UIThinFontMake(18);
            [temp setBackgroundImage:[UIImage qmui_imageWithColor:UIColorMake(102, 186, 141)] forState:UIControlStateNormal];
            [temp setBackgroundImage:[UIImage qmui_imageWithColor:UIColorMake(187, 187, 187)] forState:UIControlStateDisabled];
            temp.titleLabel.textAlignment = NSTextAlignmentCenter;
        }];
        
        
        
        [_codeButton addTarget:self
                        action:@selector(codeButtonAction:)
              forControlEvents:UIControlEventTouchUpInside];
    }
    return _codeButton;
}

- (UIButton *) confirmButton
{
    if (!_confirmButton)
    {
        _confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _confirmButton.frame = CGRectMake(0, 0, 251, 44);
        _confirmButton.enabled = NO;
        _confirmButton.layer.cornerRadius = 6;
        _confirmButton.layer.masksToBounds = YES;
        
        [_confirmButton setStyle:^(UIView *target) {
            UIBUTTONSTYLE;
            [temp setTitle:STRING.confirm forState:UIControlStateNormal];
            [temp setBackgroundImage:[UIImage qmui_imageWithColor:UIColorMake(102, 186, 141)] forState:UIControlStateNormal];
            
            [temp setBackgroundImage:[UIImage qmui_imageWithColor:UIColorMake(187, 187, 187)] forState:UIControlStateDisabled];
            temp.titleLabel.font = UIMediumFontMake(17);
        }];
        
        [_confirmButton addTarget:self
                           action:@selector(confirmButtonAction:)
                 forControlEvents:UIControlEventTouchUpInside];
    }
    return _confirmButton;
}

@end
